﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddAttributePage.aspx.cs" Inherits="Easy.Web.Pages.EntityPages.AddAttributePage" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link href="/_css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="/_css/common.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="/_css/iconfont.css" />
    <script src="/_js/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="/_js/bootstrap.min.js" type="text/javascript"></script>
    <script src="/_js/json2.js" type="text/javascript"></script>
    <script type="text/javascript">
        function addAttribute(type)
        {
            var picklistvalue = "";
            $("#picklist_table tr[id]").each(function ()
            {
                var value = $(this).find("td").eq(0).children().val();
                var key = $(this).find("td").eq(1).children().val();
                var order = $(this).find("td").eq(2).children().val();
                picklistvalue += value + "α" + key + "α" + order + "β";
            });


            var add = {};
            add["EntityId"] = $("#entityid").val();
            add["name"] = $("#name").val();
            add["DisplayName"] = $("#DisplayName").val();
            add["type"] = $("#type").val();
            add["length"] = $("#length").val();
            add["Cansearchable"] = $("#Cansearchable").val();
            add["IsNullable"] = $("#IsNullable").val();
            add["picklistvalue"] = picklistvalue;
            add["lookupentity"] = $("#entitylist").val();
            add["IsGlobalPicklist"] = $("#IsGlobalPicklist").val();
            add["GlobalPicklist"] = $("#GlobalPicklist").val();

            $.ajax({
                url: "/Service/Entity/AddAttribute.ashx",
                async: false,
                type: "post",
                dataType: "JSON",
                data: {
                    "addAttribute": JSON.stringify(add)
                },
                success: function (data, textStatus)
                {
                    if (data.Result == "0")
                    {
                        if (type == 1)
                        {
                            window.location = "AttributeListPage.aspx?Id=" + $("#entityid").val();
                        } else if (type == 2)
                        {
                            window.location = "AddAttributePage.aspx?Id=" + $("#entityid").val();
                        }
                    }
                    else
                    {
                        alert(data.Remark);
                    }

                },
                error: function (data, textStatus)
                {
                    alert("错误");

                }

            });
        }

        function updateAttribute(type)
        {
            var picklistvalue = "";
            $("#picklist_table tr[id]").each(function ()
            {
                var picklistid = $(this).attr("picklistid");
                var value = $(this).find("td").eq(0).children().val();
                var key = $(this).find("td").eq(1).children().val();
                var order = $(this).find("td").eq(2).children().val();
                picklistvalue += picklistid + "α" + value + "α" + key + "α" + order + "β";
            });


            var update = {};
            update["AttributeId"] = $("#AttributeId").val();
            update["DisplayName"] = $("#DisplayName").val();
            update["Cansearchable"] = $("#Cansearchable").val();
            update["IsNullable"] = $("#IsNullable").val();
            update["picklistvalue"] = picklistvalue;


            $.ajax({
                url: "/Service/Entity/UpdateAttribute.ashx",
                async: false,
                type: "post",
                dataType: "JSON",
                data: {
                    "updateAttribute": JSON.stringify(update)
                },
                success: function (data, textStatus)
                {
                    if (data.Result == "0")
                    {
                        if (type == 1)
                        {
                            window.location = "AttributeListPage.aspx?Id=" + $("#entityid").val();
                        }
                        else
                        {
                            location.reload();
                        }
                    }
                    else
                    {
                        alert(data.Remark);
                    }

                },
                error: function (data, textStatus)
                {
                    alert("错误");

                }

            });
        }

        function btn_return()
        {
            window.location = "AttributeListPage.aspx?Id=" + $("#entityid").val();

        }


        function type_change()
        {
            if ($("#type").val() == "nvarchar")
            {
                $("#length_div").show();
            }
            else
            {
                $("#length_div").hide();
            }

            if ($("#type").val() == "picklist")
            {
                $("#picklist_div").show();
                if($("#edit").val() == "0")
                {
                    IsGlobalPicklist_change();
                }
            }
            else
            {
                $("#picklist_div").hide();
            }

            if ($("#type").val() == "lookup")
            {
                $("#lookup_div").show();
                if ($("#edit").val() == "0")
                {
                    GetEntityList();
                }
            }
            else
            {
                $("#lookup_div").hide();
            }



        }

        $(document).ready(function ()
        {




            if ($("#edit").val() == "0")
            {
                //新建
                $("#page_title").html("新建字段");
                $("[noModify = noModify]").removeAttr("disabled");
                $("#add_btn_div").show();
                $("#update_btn_div").hide();
            }
            else if ($("#edit").val() == "1")
            {

                //修改
                getAttributeInfo();
                $("#page_title").html("修改字段");
                $("[noModify = noModify]").attr('disabled', 'true');
                $("#add_btn_div").hide();
                $("#update_btn_div").show();
            }
            else
            {
                alert("edit状态错误");
                window.location = "AttributeListPage.aspx?Id=" + $("#entityid").val();
            }
            type_change();

        });


        function btnAddRow()
        {

            var rownum = $("#picklist_table tr").length - 1;

            var value = "<input type='text' id='value_" + rownum + "' name='value_" + rownum + "' width='100%'/>";
            var key = "<input type='text' id='key_" + rownum + "' name='key_" + rownum + "' width='100%'/>";
            var order = "<input type='text' id='order_" + rownum + "' name='order_" + rownum + "' width='50px'/>";
            var button = "<button type='button' class='btn btn-primary ng-binding' onclick='DelRow(" + rownum + ");'>删除</button>";
            var row = "<tr id='" + rownum + "' ><td>" + value + "</td><td>" + key + "</td><td>" + order + "</td><td>" + button + "</td></tr>";
            $(row).insertAfter($("#picklist_table tr:eq(" + rownum + ")"));
        }

        function DelRow(rownum)
        {

            $("#picklist_table tr[id=" + rownum + "]").remove();
        }

        function getAttributeInfo()
        {
            $.ajax({
                url: "/Service/Entity/GetAttributeList.ashx",
                async: false,
                type: "post",
                dataType: "JSON",
                data: {
                    "AttributeId": $("#AttributeId").val()
                },
                success: function (data, textStatus)
                {
                    $("#DisplayName").val(data.DisplayName);

                    if (data.name.indexOf($("#prefix").html()) == -1)
                    {
                        $("#prefix").html("");
                        $("#name").val(data.name);
                    }
                    else
                    {
                        $("#name").val(data.name.substring(data.name.indexOf('_') + 1, data.name.length));
                    }


                    $("#IsNullable").val(data.IsNullable);
                    $("#Cansearchable").val(data.Cansearchable);
                    $("#type").val(data.type);
                    $("#length").val(data.length);
                    $("#IsGlobalPicklist").val(data.IsGlobalPicklist);
                    IsGlobalPicklist_change();
                    if (data.type == "picklist" && data.IsGlobalPicklist == 0)
                    {
                        var arr1 = data.picklistvalue.split('β');
                        for (var i = 0; i < arr1.length - 1; i++)
                        {
                            var arr2 = arr1[i].split('α');
                            var rownum = $("#picklist_table tr").length - 1;

                            var value = "<input type='text' id='value_" + rownum + "' value='" + arr2[1] + "' name='value_" + rownum + "' width='100%'/>";
                            var key = "<input type='text' id='key_" + rownum + "' value='" + arr2[2] + "' name='key_" + rownum + "' width='100%'/>";
                            var order = "<input type='text' id='order_" + rownum + "' value='" + arr2[3] + "' name='order_" + rownum + "' width='50px'/>";

                            var button = "<button type='button' class='btn btn-primary ng-binding' onclick='DelRow(" + rownum + ");'>删除</button>";
                            var row = "<tr id='" + rownum + "' picklistid='" + arr2[0] + "' ><td>" + value + "</td><td>" + key + "</td><td>" + order + "</td><td>" + button + "</td></tr>";
                            $(row).insertAfter($("#picklist_table tr:eq(" + rownum + ")"));
                        }
                    }
                    else if (data.type == "picklist" && data.IsGlobalPicklist == 1)
                    {
                        $("#GlobalPicklist").val(data.GlobalPicklist);
                    }
                    if (data.type == "lookup")
                    {

                        $("#entitylist").html("<option value='" + data.lookupentity + "'>" + data.lookupentity + "</option>");
                    }

                },
                error: function (data, textStatus)
                {
                    alert("错误");

                }

            });
        }

        function GetEntityList()
        {
            $.ajax({
                url: "/Service/Entity/GetEntityList.ashx",
                async: false,
                type: "post",
                dataType: "json",
                data: {
                },
                success: function (data, textStatus)
                {

                    var SMShtml = "";

                    for (var i = 0; i < data.length; i++)
                    {
                        var item = data[i];

                        //if ($("#entityid").val() != item.EntityMapId) {


                        SMShtml += "<option value='" + item.EntityName + "'>" + item.EntityDisplayName + "</option>";
                        //}

                    }



                    $("#entitylist").html(SMShtml);
                },
                error: function (data, textStatus)
                {
                    alert("ajax错误");

                }

            });

        }
        function IsGlobalPicklist_change()
        {
            if ($("#IsGlobalPicklist").val() == "0")
            {

                $("#newPicklist_div").show();
                $("#globalPicklist_div").hide();
            }
            else
            {
                $("#newPicklist_div").hide();

                $("#globalPicklist_div").show();
                getGlobalPicklist();
            }
        }

        function getGlobalPicklist()
        {
            $.ajax({
                url: "/Service/Entity/GetGlobalPicklist.ashx",
                async: false,
                type: "post",
                dataType: "json",
                data: {
                },
                success: function (data, textStatus)
                {

                    var SMShtml = "";

                    for (var i = 0; i < data.length; i++)
                    {
                        var item = data[i];



                        SMShtml += "<option value='" + item.Name + "'>" + item.DisplayName + "</option>";


                    }



                    $("#GlobalPicklist").html(SMShtml);
                },
                error: function (data, textStatus)
                {
                    alert("ajax错误");

                }

            });
        }


        function btnAddNewPicklist()
        {
            var rv = window.showModalDialog("../EntityPages/AddGlobalStringMapPage.aspx?edit=2", "", "dialogWidth=600px;dialogHeight=500px;center=yes;resizable=yes");
            getGlobalPicklist();
            $("#GlobalPicklist").val(rv);
        }
    </script>
    <style>
        .page-header{margin:15px;}
        .page-header+.btn-primary{margin:0px 15px 10px 5px;}
        .panel-heading{padding:3px 15px;height:40px;line-height:34px;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <input id="edit" type="hidden" value="<%= edit %>" />
        <input id="entityid" type="hidden" value="<%=_EntityId %>" />
        <input id="AttributeId" type="hidden" value="<%=AttributeId %>" />
        <div class="">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header" id="page_title">新建字段</h1>
                    <button type='button' style="float: right" id='Button1' class='btn btn-primary ng-binding' onclick="btn_return();">
                        取消</button>
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
            <div class="row">
                <div class="col-lg-6">
                    <div class="panel panel-default">
                        <!-- /.panel-heading -->
                        <div class="panel-body">
                            <div class="form-group">
                                <label for="DisplayName">
                                    字段中文名</label>
                                <input type="text" class="form-control" id="DisplayName" />
                            </div>
                            <div class="form-group">
                                <label for="name">
                                    字段英文名</label>
                                <div class="input-group">
                                    <div class="input-group-addon">
                                        <span id="prefix"><%=Prefix%>_</span>

                                    </div>
                                    <input nomodify="noModify" type="text" class="form-control" id="name" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="IsNullable">
                                    需求级别</label>
                                <select class="form-control" id="IsNullable">
                                    <option value="1" selected>无约束的</option>
                                    <option value="0">业务必需的</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="Cansearchable">
                                    可搜索</label>
                                <select class="form-control" id="Cansearchable">
                                    <option value="1" selected>是</option>
                                    <option value="0">否</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="type">
                                    字段类型</label>
                                <select class="form-control" id="type" onchange="type_change();" nomodify="noModify">
                                    <option value="nvarchar">nvarchar</option>
                                    <option value="picklist">picklist</option>
                                    <option value="bit">bit</option>
                                    <option value="int">int</option>
                                    <option value="float">float</option>
                                    <option value="ntext">ntext</option>
                                    <option value="datetime">datetime</option>
                                    <option value="date">date</option>
                                    <option value="lookup">lookup</option>
                                    <option value="password">password</option>
                                    <option value="image">image</option>
                                </select>
                            </div>
                            <div class="form-group" id="length_div" style="display: none;">
                                <label for="length">
                                    长度</label>
                                <input type="text" class="form-control" id="length" nomodify="noModify" />
                            </div>
                            <div class="form-group" id="lookup_div" style="display: none;">
                                <label for="type">
                                    实体</label>
                                <select class="form-control" id="entitylist" nomodify="noModify">
                                </select>
                            </div>
                            <div class="form-group" id="add_btn_div" style="display: none;">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <button type="button" class="btn btn-primary ng-binding" onclick="addAttribute(1);">
                                        保存并关闭</button>
                                    <button type="button" class="btn btn-primary ng-binding" onclick="addAttribute(2);">
                                        保存并新建</button>
                                </div>
                            </div>
                            <div class="form-group" id="update_btn_div" style="display: none;">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <button type="button" class="btn btn-primary ng-binding" onclick="updateAttribute(1);">
                                        保存并关闭</button>
                                    <button type="button" class="btn btn-primary ng-binding" onclick="updateAttribute(2);">
                                        保存</button>
                                </div>
                            </div>
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <div class="col-lg-6">
                    <div class="panel panel-default" id="picklist_div" style="display: none;">
                        <!-- /.panel-heading -->
                        <div class="panel-body">
                            <div class="form-group">
                                <label for="IsGlobalPicklist">
                                    picklist来源</label>
                                <select class="form-control" id="IsGlobalPicklist" disabled="disabled">
                                    <option value="1">从通用选项集读取</option>
                                    <option value="0">新建</option>
                                </select>
                            </div>
                            <div class="form-group" id="newPicklist_div" style="display: none;">
                                <button type="button" class="btn btn-primary ng-binding" onclick="btnAddRow();">
                                    添加</button>
                                <table class="table table-condensed" id="picklist_table">
                                    <thead>
                                        <tr>
                                            <th>值
                                            </th>
                                            <th>标签
                                            </th>
                                            <th>排序
                                            </th>
                                            <th>操作
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>

                            <div class="form-group" id="globalPicklist_div" style="display: none;">
                                <label for="GlobalPicklist">
                                    picklist来源</label>
                                <select class="form-control" id="GlobalPicklist"
                                    nomodify="noModify">
                                </select>

                                <button type="button" class="btn btn-primary ng-binding" onclick="btnAddNewPicklist();">
                                    添加通用选项集</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</body>
</html>
